<template>
	<view class="normal-login-container">
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model.trim="form.userName" type="tel" class="input" placeholder="请输入手机号(登录账号)"
					maxlength="11" />
			</view>

			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model.trim="form.password" password class="input" placeholder="请输入新密码" maxlength="16" />
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model.trim="form.confirmPassword" password class="input" placeholder="请再次输入新密码"
					maxlength="16" />
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont icon-code icon"></view>
				<input v-model.trim="form.code" inputmode="numeric" class="input" style="width: 60%;"
					placeholder="请输入短信验证码" maxlength="6" />
				<view class="btn" :class="{'btn-disable':seconds!==0}" ref="codeDom" @click="sendCode()">
					{{codeText}}
				</view>
			</view>

			<view class="action-btn">
				<button @click="handle()" class="login-btn cu-btn block bg-blue lg">立即重置</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		pwdVerifyCode,
		findPassword
	} from '@/api/login'

	export default {

		data() {
			return {
				seconds: 0,
				codeText: "发送验证码", //验证码文字
				form: {
					userName: "",
					password: "",
					confirmPassword: "",
					code: ""
				}
			}
		},
		methods: {
			verify() {
				var un = this.form.userName
				var pw = this.form.password
				if (!un) {
					this.$modal.msgError("请输入手机号！")
					return false
				} else if (!(/^[1][3-9][0-9]{9}$/.test(un))) {
					this.$modal.msgError("手机号格式异常！")
					return false
				} else if (pw == '' || pw.length < 6) {
					this.$modal.msgError("密码为空或长度小于6位")
					return false
				} else if (!(/^[_a-zA-Z0-9]+$/.test(pw))) {
					this.$modal.msgError("密码仅由英文字母，数字以及下划线组成")
					return false
				} else if (pw !== this.form.confirmPassword) {
					this.$modal.msgError("两次输入的密码不一致")
					return false
				}
				return true
			},
			sendCode() {
				if (this.seconds < 1 && this.verify()) {
					pwdVerifyCode(this.form.userName).then(res => {
						if (res.code == 200) {
							this.seconds = 59
							let timer = setInterval(() => {
								this.seconds--;
								this.codeText = "重新获取(" + this.seconds + ")"
								if (this.seconds <= 0) {
									this.codeText = "发送验证码"
									clearInterval(timer)
								}
							}, 1000)
						} else {
							this.$modal.msgError(res.msg);
						}
					})
				}
			},

			handle() {
				if (this.verify()) {
					this.$modal.loading("重置中，请等待...")
					findPassword(this.form).then(res => {
						console.log('res', res)
						if (res.code == 200) {
							this.form = {
								userName: "",
								password: "",
								confirmPassword: "",
								code: ""
							}
							this.$modal.msgSuccess(res.msg)
						} else {
							this.$modal.msgError(res.msg)
						}
						this.$modal.closeLoading()
					}).catch((e) => {
						console.error(e)
					})
				}
			}

		}
	}
</script>

<style lang="scss">
	page {
		background: linear-gradient(180deg, #c4d6ff, #f7f8fa);
	}

	.normal-login-container {
		width: 100%;
		// height: auto;
		background-image: url("@/static/images/register-bg.png");
		background-repeat: no-repeat;
		background-size: contain;
		background-position: center top;
		padding-top: 45%;

		.btn {
			border-radius: 10rpx;
			font-size: 20rpx;
			color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;
			background: #4095E5;
			height: 50rpx;
			width: 180rpx;
		}

		.btn-disable {
			background-color: #999;
		}

		.login-form-content {
			width: 90%;
			margin: auto;
			padding: 10px;
			border-radius: 10px;
			background-color: #fff;

			.input-item {
				margin: 10px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 10px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					background-color: #f5f6f7;
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}
			}

			.login-btn {
				margin-top: 20px;
				height: 45px;
			}

			.reg {
				margin-top: 15px;
			}

			.xieyi {
				color: #333;
				margin-top: 10px;
			}

			.login-code {
				height: 38px;
				float: right;

				.login-code-img {
					height: 38px;
					position: absolute;
					margin-left: 10px;
					width: 200rpx;
				}
			}

			.sub-info {
				margin-top: 20px;
				display: flex;
				flex-direction: column;
				align-items: center;
			}

			.sub-info uni-icons {
				margin-top: 10px;
				color: red;
			}

			.sub-info text {
				margin-top: 10px;
				color: #919499;
				font-size: 12px;
			}

			.sub-info p {
				font-size: 13px;
				color: #303030;
				display: flex;
				flex-direction: row;
				align-items: center;
			}

			.sub-info p span {
				display: block;
				width: 90px;
				height: 2px;
				background-image: linear-gradient(to left, #606060, #f4eacc);
				margin: 0 8px;
			}

			.sub-info p span.s2 {
				background-image: linear-gradient(to left, #f4eacc, #606060);
			}
		}
	}
</style>